package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportServece;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class ReportServiceImpl implements ReportServece {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    private List<LocalDate> getDateList(LocalDate begin, LocalDate end){
        List<LocalDate> list = new ArrayList<>();
        list.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            list.add(begin);
        }
        return list;
    }
    private  LocalDateTime getMinLocalDateTime(LocalDate date){
        return LocalDateTime.of(date, LocalTime.MIN);
    }
    private  LocalDateTime getMaxLocalDateTime(LocalDate date){
        return LocalDateTime.of(date, LocalTime.MAX);
    }
    private String listToString(List list){
        return StringUtils.join(list,",");
    }
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<Double> turnoverList = new ArrayList<>();
        List<LocalDate> dateList = getDateList(begin, end);
        dateList.forEach(e->{
            LocalDateTime min = getMinLocalDateTime(e);
            LocalDateTime max = getMaxLocalDateTime(e);
            Double turnover = orderMapper.turnoverStatistics(min,max, Orders.COMPLETED);
            turnover = turnover != null ? turnover : 0d;
            turnoverList.add(turnover);
        });
        return TurnoverReportVO.builder().dateList(listToString(turnoverList)).turnoverList(listToString(turnoverList)).build();
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDateList(begin, end);
        List<Integer> totalUserList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();
        dateList.forEach(e->{
            LocalDateTime min = getMinLocalDateTime(begin);
            LocalDateTime max = getMaxLocalDateTime(end);
            Integer newUser = userMapper.userStatistics(min,max);
            Integer totalUser = userMapper.userStatistics(null,max);
            newUser = newUser != null ? newUser : 0;
            totalUser = totalUser != null ? totalUser : 0;
            newUserList.add(newUser);
            totalUserList.add(totalUser);
        });
        return UserReportVO.builder().dateList(listToString(dateList)).newUserList(listToString(newUserList)).totalUserList(listToString(totalUserList)).build();
    }

    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDateList(begin, end);
        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validOrderCountList = new ArrayList<>();
        int totalOrderCount = 0;
        int validOrderCount = 0;
        double orderCompletionRate = 0D;
        for (LocalDate e : dateList) {
            LocalDateTime min = getMinLocalDateTime(e);
            LocalDateTime max = getMaxLocalDateTime(e);
            Integer dailyTotalOrderCount = orderMapper.orderStatistics(min,max,null);
            Integer dailyValidOrderCount = orderMapper.orderStatistics(min,max,Orders.COMPLETED);
            dailyTotalOrderCount = dailyTotalOrderCount != null ? dailyTotalOrderCount : 0;
            dailyValidOrderCount = dailyValidOrderCount != null ? dailyValidOrderCount : 0;
            totalOrderCount += dailyTotalOrderCount;
            validOrderCount += dailyValidOrderCount;
            orderCountList.add(dailyTotalOrderCount);
            validOrderCountList.add(dailyValidOrderCount);
        }
        if(totalOrderCount != 0)orderCompletionRate = orderCompletionRate*1d/totalOrderCount;
        return OrderReportVO.builder().dateList(listToString(dateList)).orderCompletionRate(orderCompletionRate)
                .orderCountList(listToString(orderCountList)).totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount).validOrderCountList(listToString(validOrderCountList)).build();
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        List<String> nameList = new ArrayList<>();
        List<Integer> numberList = new ArrayList<>();
        List<GoodsSalesDTO> goodsSalesDTOList = orderMapper.getSalesTop10(getMinLocalDateTime(begin), getMaxLocalDateTime(end));
        goodsSalesDTOList.forEach(e->{
            nameList.add(e.getName());
            numberList.add(e.getNumber());
        });
        return SalesTop10ReportVO.builder().nameList(listToString(nameList)).numberList(listToString(nameList)).build();
    }
}
